package com.ln.adoms.entity;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;

import com.ln.adoms.entity.neum.FullStatus;
import com.ln.adoms.entity.neum.Status;
import com.ln.core.orm.hibernate.BaseEntity;
import com.ln.core.security.entity.Users;
import com.ln.core.utils.MyUtils;

@Entity
@Table(name = "order_order")
public class Order extends BaseEntity {

    private String name; // 订单名称
    private String code; // 订单编号
    private Double totalCost; // 总价
    private String phone; // 订单创建人联系电话
    private Date receivedTime; // 订单处理时间
    private String orderChargeUser; // 订单处理人
    private Date orderCreateTime; // 订单生成时间
    private Status status; // 订单状态
    private FullStatus fStatus; // 订单状态
    private boolean type = true; // true是正常订单，false是退货单

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "department_id")
    private Department department; // 订单所属部门

    @Column(length = 500)
    private String memo; // 备注
    @Column(length = 500)
    private String memo2; // 备注
    @Column(length = 500)
    private String memo3; // 用于发货备注
    @Column(length = 500)
    private String memo4; // 用于发货备注
    private String receivedUser; // 收货人
    private Date receivedDate; // 收货时间

    private String outUser; // 发货人
    private Date outTime; // 订单生成时间
    private Date autoReceiveTime; // 订单自动收货时间

    @Column(length = 1000)
    private String orderInfo;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "department_target_id")
    private Department departmentTarget; // 订单目标部门

    private String createUser; // 订单创建人
    private Integer goodsNumber = 0; // 订单商品数量
    private Double boxNumber = 0.0; // 订单商品数量
    @Column(length = 2000)
    private String logs;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "marchandise_id")
    private Marchandise marchandise;// 订购商品

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Double getTotalCost() {
        return totalCost;
    }

    public void setTotalCost(Double totalCost) {
        this.totalCost = totalCost;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Date getReceivedTime() {
        return receivedTime;
    }

    public void setReceivedTime(Date receivedTime) {
        this.receivedTime = receivedTime;
    }

    public String getOrderChargeUser() {
        return orderChargeUser;
    }

    public void setOrderChargeUser(String orderChargeUser) {
        this.orderChargeUser = orderChargeUser;
    }

    public Date getOrderCreateTime() {
        return orderCreateTime;
    }

    public void setOrderCreateTime(Date orderCreateTime) {
        this.orderCreateTime = orderCreateTime;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public String getMemo() {
        return memo;
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }

    public Department getDepartmentTarget() {
        return departmentTarget;
    }

    public void setDepartmentTarget(Department departmentTarget) {
        this.departmentTarget = departmentTarget;
    }

    public String getCreateUser() {
        return createUser;
    }

    public void setCreateUser(String createUser) {
        this.createUser = createUser;
    }

    public Integer getGoodsNumber() {
        return goodsNumber == null ? 0 : goodsNumber;
    }

    public void setGoodsNumber(Integer goodsNumber) {
        this.goodsNumber = goodsNumber;
    }

    public Marchandise getMarchandise() {
        return marchandise;
    }

    public void setMarchandise(Marchandise marchandise) {
        this.marchandise = marchandise;
    }

    public String getMemo2() {
        return memo2;
    }

    public void setMemo2(String memo2) {
        this.memo2 = memo2;
    }

    public String getMemo3() {
        return memo3;
    }

    public void setMemo3(String memo3) {
        this.memo3 = memo3;
    }

    public String getOutUser() {
        return outUser;
    }

    public void setOutUser(String outUser) {
        this.outUser = outUser;
    }

    public Date getOutTime() {
        return outTime;
    }

    public void setOutTime(Date outTime) {
        this.outTime = outTime;
    }

    public boolean isType() {
        return type;
    }

    public void setType(boolean type) {
        this.type = type;
    }

    public String getOrderInfo() {
        return orderInfo;
    }

    public void setOrderInfo(String orderInfo) {
        this.orderInfo = orderInfo;
    }

    public String getLogs() {
        return logs;
    }

    public void setLogs(String logs) {
        this.logs = logs;
    }

    public String getMemo4() {
        return memo4;
    }

    public void setMemo4(String memo4) {
        this.memo4 = memo4;
    }

    public String getReceivedUser() {
        return receivedUser;
    }

    public void setReceivedUser(String receivedUser) {
        this.receivedUser = receivedUser;
    }

    public Date getReceivedDate() {
        return receivedDate;
    }

    public void setReceivedDate(Date receivedDate) {
        this.receivedDate = receivedDate;
    }

    public Double getBoxNumber() {
        return boxNumber == null ? 0.0 : boxNumber;
    }

    public void setBoxNumber(Double boxNumber) {
        this.boxNumber = boxNumber;
    }

    public void setLogsMap(Object key, Object value) {
        try {
            Map<Object, Object> map = this.getLogsMap();
            map.put(key, value);
            ObjectMapper mapper = new ObjectMapper();
            this.logs = mapper.writeValueAsString(map);
        } catch (Exception e) {
        }
    }

    public void addLogs(Users u, String msg) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        Date d = new Date();
        map.put("date", MyUtils.format.format(d));
        map.put("user", u.getName());
        map.put("department", u.getDepartment().getName());
        map.put("msg", msg);
        map.put("status", this.fStatus.v());
        this.setLogsMap(String.valueOf(d.getTime()), map);
    }

    @SuppressWarnings("unchecked")
    public Map<Object, Object> getLogsMap() {
        Map<Object, Object> map = new LinkedHashMap<Object, Object>();
        ObjectMapper mapper = new ObjectMapper();
        String s = this.logs;
        try {
            if (StringUtils.isNotBlank(s)) {
                map = mapper.readValue(s, Map.class);
            }
        } catch (Exception e) {
        }
        return map;
    }

    public FullStatus getfStatus() {
        return fStatus;
    }

    public Date getAutoReceiveTime() {
        return autoReceiveTime;
    }

    public void setAutoReceiveTime(Date autoReceiveTime) {
        this.autoReceiveTime = autoReceiveTime;
    }

    public void setfStatus(FullStatus fStatus) {
        this.fStatus = fStatus;
    }

}